<template>
  <div id="homepage" class="homepage">
    <el-row :gutter="20">
      <el-col :span="6">
        <div class="grid-content bg-purple">
          <div class="head-style">
            <span class="header-font">库存总数量</span>
          </div>
          <el-divider class="divider"></el-divider>
          <el-row>
            <el-col :span="24" style="padding-left: 0px; padding-right: 0px">
              <div class="div-style">
                <div
                  class="left_icon el-icon-receiving"
                  style="color: #f7c114; margin-right: 10px"
                ></div>
                <span class="span-style" style="color: #f7c114">{{
                  reportData.allStockQty
                }}</span>
              </div>
            </el-col>
            <!-- <el-col :span="6" style="padding-left: 0px; padding-right: 0px">
              <el-card shadow="never">
                <div class="div-style">
                  <span class="rightSpan">比昨天</span>
                </div>
              </el-card>
            </el-col> -->
          </el-row>
        </div>
      </el-col>

      <el-col :span="6">
        <div class="grid-content bg-purple">
          <div class="head-style">
            <span class="header-font">SKU总数量</span>
          </div>

          <el-divider class="divider"></el-divider>

          <el-col :span="24" style="padding-left: 0px; padding-right: 0px">
            <div class="div-style">
              <div
                class="left_icon el-icon-s-grid"
                style="color: #65318e; margin-right: 10px"
              ></div>
              <span class="span-style" style="color: #65318e">{{
                reportData.allSKUQty
              }}</span>
            </div>
          </el-col>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="grid-content bg-purple">
          <div class="head-style">
            <span class="header-font">今日入库订单数</span>
          </div>

          <el-divider class="divider"></el-divider>

          <el-col :span="24" style="padding-left: 0px; padding-right: 0px">
            <div class="div-style">
              <div
                class="left_icon el-icon-upload2"
                style="color: #9ba88d; margin-right: 10px"
              ></div>
              <span class="span-style" style="color: #9ba88d">{{
                reportData.inBillNum
              }}</span>
            </div>
          </el-col>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="grid-content bg-purple">
          <div class="head-style">
            <span class="header-font">今日出库订单数</span>
          </div>

          <el-divider class="divider"></el-divider>

          <el-col :span="24" style="padding-left: 0px; padding-right: 0px">
            <div class="div-style">
              <div
                class="left_icon el-icon-download"
                style="color: #007b43; margin-right: 10px"
              ></div>
              <span class="span-style" style="color: #007b43">{{
                reportData.outBillNum
              }}</span>
            </div>
          </el-col>
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="8">
        <div class="top-content bg-purple">
          <div class="tag">
            <span style="padding-left: 20px; font-size: 15px; font-weight: 600"
              >库存动态</span
            >
          </div>
          <el-divider class="divider1"></el-divider>
          <el-row>
            <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
              <div class="box">
                <div class="box3" style="position: relative">
                  <div
                    id="main"
                    style="width: 100%; height: 400px; margin-top: 60px"
                  ></div>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="top-content bg-purple">
          <div class="tag">
            <span style="padding-left: 20px; font-size: 15px; font-weight: 600"
              >库位利用率</span
            >
          </div>
          <el-divider class="divider1"></el-divider>

          <div
            id="main2"
            style="width: 100%; height: 400px; margin-top: 60px"
          ></div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="top-content bg-purple">
          <div class="tag">
            <span style="padding-left: 20px; font-size: 15px; font-weight: 600"
              >SKU占比TOP5</span
            >
          </div>
          <el-divider class="divider1"></el-divider>
          <div
            id="main3"
            style="width: 100%; height: 400px; margin-top: 60px"
          ></div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
var echarts = require("echarts");

export default {
  name: "Homepage",
  data() {
    return {
      radio2: 0,
      //折线图
      option: {
        color: ["#0090FF", "#36CE9E"],
        title: {
          text: "",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["入库", "出库"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          // data: ["周一", "周二", "周三", "周四", "周五"],
          data: [],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            smooth: true,
            name: "入库",
            type: "line",

            // data: [120, 132, 101, 134, 90],
            data: [],
          },
          {
            smooth: true,
            name: "出库",
            type: "line",

            data: [220, 182, 191, 234, 290],
          },
        ],
      },
      option2: {
        series: [
          {
            type: "gauge",
            axisLine: {
              lineStyle: {
                width: 30,
                color: [
                  [0.3, "#67e0e3"],
                  [0.7, "#37a2da"],
                  [1, "#fd666d"],
                ],
              },
            },
            pointer: {
              itemStyle: {
                color: "auto",
              },
            },
            axisTick: {
              distance: -30,
              length: 8,
              lineStyle: {
                color: "#fff",
                width: 2,
              },
            },
            splitLine: {
              distance: -30,
              length: 30,
              lineStyle: {
                color: "#fff",
                width: 4,
              },
            },
            axisLabel: {},
            detail: {
              valueAnimation: true,
              formatter: "{value} %",
              color: "auto",
            },
            data: [
              {
                value: 55,
              },
            ],
          },
        ],
      },
      //饼状图
      option3: {
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c}",
        },
        toolbox: {
          feature: {
            dataView: { readOnly: false },
            restore: {},
            saveAsImage: {},
          },
        },
        legend: {
          data: ["Show", "Click", "Visit", "Inquiry", "Order"],
        },
        series: [
          {
            name: "",
            type: "funnel",
            left: "10%",
            top: 60,
            bottom: 60,
            width: "80%",
            min: 0,
            max: 100,
            minSize: "0%",
            maxSize: "100%",
            sort: "descending",
            gap: 2,
            label: {
              show: true,
              position: "inside",
            },
            labelLine: {
              length: 10,
              lineStyle: {
                width: 1,
                type: "solid",
              },
            },
            itemStyle: {
              borderColor: "#fff",
              borderWidth: 1,
            },
            emphasis: {
              label: {
                fontSize: 18,
              },
            },
            data: [
              //       { value: 60, name: 'Visit' },
              // { value: 40, name: 'Inquiry' },
              // { value: 20, name: 'Order' },
              // { value: 80, name: 'Click' },
              // { value: 100, name: 'Show' }
            ],
          },
        ],
      },
      //看板数据
      reportData: {
        allStockQty: 0,
        allSKUQty: 0,
        inBillNum: 0,
        outBillNum: 0,
        cellRatio: 0.0,
      },
    };
  },
  methods: {
    initEcharts() {
      var myChart = echarts.init(document.getElementById("main"));
      var myChart2 = echarts.init(document.getElementById("main2"));
      var myChart3 = echarts.init(document.getElementById("main3"));
      // 指定图表的配置项和数据

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(this.option);
      myChart2.setOption(this.option2);
      myChart3.setOption(this.option3);
    },
  },
  mounted() {
    this.initEcharts();
  },
  //加载数据
  created() {
    var that = this;
    var url = localStorage.getItem("baseUrl");
    let token = localStorage.getItem("token");
    let wareId = PF.getLocal("wareId", "number");

    let param = { wareId: wareId };

    $.ajax({
      //jQuery方法，此处可以换成其它请求方式
      url: url + "/inventorys/report/data?wareId=" + wareId,
      dataType: "json",
      type: "get",
      async: false,
      headers: {
        Accept: "*/*",
        "access-token": token,
      },
      data: {},
      beforeSend: function () {},
      contentType: "application/json;charset=utf-8", //这个参数也是header参数
      processData: false, //不去处理发送的数据

      error: function (res) {
        console.log(res);
      },
      success: function (res) {
        that.reportData = res.data;
        that.option2.series[0].data[0].value = that.reportData.cellRatio;
        var skuStockList = [];
        for (var i = 0; i < res.data.topSku.length; i++) {
          skuStockList.push({
            value: res.data.topSku[i].quantity,
            name: res.data.topSku[i].itemName,
          });
        }
        that.option3.series[0].data = skuStockList;
      },
    });

    //获取出入库订单数
    $.ajax({
      //jQuery方法，此处可以换成其它请求方式
      url: url + "/transfer/report/data?wareId=" + wareId,
      dataType: "json",
      type: "get",
      async: false,
      headers: {
        Accept: "*/*",
        "access-token": token,
      },
      data: {},
      beforeSend: function () {},
      contentType: "application/json;charset=utf-8", //这个参数也是header参数
      processData: false, //不去处理发送的数据

      error: function (res) {
        console.log(res);
      },
      success: function (res) {
        let reportData = res.data;
        console.log(JSON.stringify(reportData));
        that.reportData.inBillNum = reportData.inBillNum;
        that.reportData.outBillNum = reportData.outBillNum;
        var inTrendKey = [];
        var inTrendValue = [];
        var outTrendValue = [];
        for (var key in reportData.inTrend) {
          var value = reportData.inTrend[key];
          inTrendKey.push(key);
          inTrendValue.push(value);
        }
        for (var key2 in reportData.outTrend) {
          var value2 = reportData.outTrend[key2];
          outTrendValue.push(value2);
        }
        that.option.xAxis.data = inTrendKey;
        that.option.series[0].data = inTrendValue;
        that.option.series[1].data = outTrendValue;
      },
    });
  },
};
</script>

<style lang="less" scoped>
.left_icon {
  font-size: 28px;
}

.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple {
  background: white;
}

.grid-content {
  border-radius: 4px;
  min-height: 150px;
}

.top-content {
  border-radius: 4px;
  min-height: 580px;
}

.head-style {
  height: 20px;
  line-height: 40px;
  text-align: center;
}
.content {
  min-height: 80px;
}
.divider {
  margin-top: 23px;
  margin-bottom: 5px;
}
.divider1 {
  margin-top: 10px;
  margin-bottom: 5px;
}
.col-style {
  padding-left: 0px;
  padding-right: 0px;
}
.div-style {
  background: white;
  height: 95px;
  line-height: 95px;
  text-align: center;
  justify-content: center;
  margin: 0 auto;
  flex-direction: column;
  .rightSpan {
    padding-left: 15px;
    width: 45px;
    margin-top: 10px;
  }
}

.div-style2 {
  height: 95px;
  text-align: right;
  justify-content: center;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

.span-style {
  font-weight: 500;
  font-size: x-large;
}
.tag {
  height: 32px;
  line-height: 40px;
}
.header-font {
  font-size: 15px;
  font-weight: 600;
}
</style>